Thread: [BEGINNER] Binary To Decimal Converter Help

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    74

    Exclamation [BEGINNER] Binary To Decimal Converter Help

    Hello guys,
    I was coding a binary to decimal converter but it doesn't work. I tried to find the bug but failed. The problem is: It works fine for numbers like 111 (7) but when I put other binary numbers it gives me wrong answers like 1011 should be 13 but it gives me 15. I feel that problem must be somewhere in line 19. Please help.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    int main()
    {
        int bin[100]={0},x,i=0,sum=0,n;
        printf("Lenght of binary number is: ");
        scanf("%d",&n);
        
        printf("What is the binary number, enter ONE DIGIT AT A TIME: ");
        for(x=0;x<n;x++)
        {
            bin[x]=scanf("%d", &bin);
        }
    
    
        for(x=0;x<n;x++)
        {
            sum=sum+bin[x]*pow(2,i);
            i++;
        }
    
        printf("The Number in decimal is : %d",sum);
        return(0);
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    EDIT: Sorry, it looks like you posted code is correct.
    I suggest verify the code be ran matches it.

    You are doing nothing to handle case of binary zero.


    Edit2: What do you think this line does?

    Code:
    bin[x]=scanf("%d", &bin);
    Code:
    warning: format '%d' expects argument of type 'int *', but argument 2 has type 'int (*)[100]' [-Wformat]
    I suggest getting a compiler/IDE that gives you warnings!

    Edit3: The thing you likely wanted to do.
    Code:
    scanf("%d", &bin[x]);
    Tim S.
    Last edited by stahta01; 09-11-2014 at 10:14 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    1
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    void main()
    {
        int rem,i=0,dec=0;
        long int bin;
        clrscr();
    
    
        printf("Enter the binary value\n");
        scanf("%ld",&bin);
        while(bin>0)
        {
            rem=bin%10;
            bin=bin/10;
            dec=dec+(rem*pow(2,i));
            i++;
        }
        printf("corresponding decimal value = %d",dec);
        getch();
    }

  4. #4
    Registered User
    Join Date
    Sep 2014
    Posts
    74
    Quote Originally Posted by stahta01 View Post
    EDIT: Sorry, it looks like you posted code is correct.
    I suggest verify the code be ran matches it.

    You are doing nothing to handle case of binary zero.


    Edit2: What do you think this line does?

    Code:
    bin[x]=scanf("%d", &bin);
    Code:
    warning: format '%d' expects argument of type 'int *', but argument 2 has type 'int (*)[100]' [-Wformat]
    I suggest getting a compiler/IDE that gives you warnings!

    Edit3: The thing you likely wanted to do.
    Code:
    scanf("%d", &bin[x]);
    Tim S.
    Now I changed the &bin to &bin[x]. But it still doesn't work

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    int main()
    {
        int bin[100],x,i=0,sum,n;
        printf("Lenght of binary number is: ");
        scanf("%d",&n);
    
        printf("What is the binary number, enter ONE DIGIT AT A TIME: \n");
        for(x=0;x<n;x++)
        {
            bin[x]=scanf("%d", &bin[x]);
        }
    
    
        for(x=0;x<n;x++)
        {
            sum=sum+bin[x]*pow(2,i);
            i++;
        }
    
        printf("The Number in decimal is : %d",sum);
        return(0);
    }

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    For starters, "sum" isn't initialized before it's used (though it was in the first version you posted).

  6. #6
    Registered User
    Join Date
    Sep 2014
    Posts
    74
    Quote Originally Posted by Matticus View Post
    For starters, "sum" isn't initialized before it's used (though it was in the first version you posted).
    It didn't work then either.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Let's look at your original problem and see if we can find some clues.

    Quote Originally Posted by Sankait Laroiya
    It works fine for numbers like 111 (7) but when I put other binary numbers it gives me wrong answers like 1011 should be 13 but it gives me 15.
    You say it "works fine for numbers like 111 (7)", but what about another three-bit number?

    Binary 101 (5) also produces 7. In fact, any three bit number seems to produce 7. What is 7 in binary? It is 111.

    Any four bit number seems to always produce 15. What is 15 in binary? 1111.

    Now we've identified a pattern. It seems your code is always behaving as if the input was all ones.

    So let's put a print statement in your second "for()" loop to print out the value of "bin[x]" to see what values it holds.

    Well! All the elements seem to be one, regardless of what we entered! So maybe the problem isn't with line 19 after all. Let's check out the input on line 13.

    Code:
    bin[x]=scanf("%d", &bin[x]);
    This looks suspicious. Normally when we use "scanf()" to read in an integer, it would look like this:

    Code:
    scanf("%d", &bin[x]);
    The "&binx[x]" tells "scanf()" where to store the input. However, your version has "bin[x]=scanf..." So you're assigning the return value of "scanf()" to "bin[x]", overwriting the value you just read. What is the return value of "scanf()"?

    Quote Originally Posted by scanf()
    On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.
    scanf()

    So "scanf()" returns the number of items it successfully read. In your case, entering one integer at a time as expected, "scanf()" returns that number of items (one) successfully read.

    That's where your array is being filled with all ones!

    The solution, as stahta01 pointed out in post #2, is to change that line to: scanf("%d", &bin[x]);
    (i.e. get rid of the "bin[x]=" part).

    And that, my friend, is how we troubleshoot.
    Last edited by Matticus; 09-12-2014 at 08:20 AM.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You might also want to read Tim's post again.

    "bin[x] = scanf("%d", &bin[x]);" will cause the value of bin[x] to have a value 1 (if a value was successfully read), 0 if no value was read, or EOF (an implementation defined value) if end-of-file is encountered after having successfully read something.

    That value has no real relationship to the binary value you are inputting.

    Even if you manage to get the digits to be read correctly, your summing loop is using them in reverse order.



    To be even more blunt: stop relying on guesswork to solve your problem.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Registered User
    Join Date
    Sep 2014
    Posts
    74
    Quote Originally Posted by Matticus View Post
    Let's look at your original problem and see if we can find some clues.



    You say it "works fine for numbers like 111 (7)", but what about another three-bit number?

    Binary 101 (5) also produces 7. In fact, any three bit number seems to produce 7. What is 7 in binary? It is 111.

    Any four bit number seems to always produce 15. What is 15 in binary? 1111.

    Now we've identified a pattern. It seems your code is always behaving as if the input was all ones.

    So let's put a print statement in your second "for()" loop to print out the value of "bin[x]" to see what values it holds.

    Well! All the elements seem to be one, regardless of what we entered! So maybe the problem isn't with line 19 after all. Let's check out the input on line 13.

    Code:
    bin[x]=scanf("%d", &bin[x]);
    This looks suspicious. Normally when we use "scanf()" to read in an integer, it would look like this:

    Code:
    scanf("%d", &bin[x]);
    The "&binx[x]" tells "scanf()" where to store the input. However, your version has "bin[x]=scanf..." So you're assigning the return value of "scanf()" to "bin[x]", overwriting the value you just read. What is the return value of "scanf()"?



    scanf()

    So "scanf()" returns the number of items it successfully read. In your case, entering one integer at a time as expected, "scanf()" returns that number of items (one) successfully read.

    That's where your array is being filled with all ones!

    The solution, as stahta01 pointed out in post #2, is to change that line to: scanf("%d", &bin[x]);
    (i.e. get rid of the "bin[x]=" part).

    And that, my friend, is how we troubleshoot.
    removed the bin[x], now it works fine. Thanks.

  10. #10
    Registered User
    Join Date
    Sep 2014
    Posts
    74
    To be even more blunt: stop relying on guesswork to solve your problem.
    Did you see [BEGINNER] tag in the post tittle? Maybe you didn't. I don't use guess work, I am learning so please guide if you want to.

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Yes, I saw the beginner tag. There is a difference between guesswork and trying things out for learning purposes. Your approach in this thread is demonstrating guesswork. Hence my guidance to stop it.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  12. #12
    Registered User
    Join Date
    Sep 2014
    Posts
    74
    Quote Originally Posted by grumpy View Post
    Yes, I saw the beginner tag. There is a difference between guesswork and trying things out for learning purposes. Your approach in this thread is demonstrating guesswork. Hence my guidance to stop it.
    Firstly, I write code accorfing to what I need. Secondly, I come here only after trying and eliminating all the bugs under my knowledge. So, if I didn't know that re-assigning bin[x] will give me a single value that's because I didn't know it as a beginner.

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Sankait Laroiya View Post
    Firstly, I write code accorfing to what I need.
    What do you mean by this? Potentially, everyone codes according to their needs.

    Secondly, I come here only after trying and eliminating all the bugs under my knowledge. So, if I didn't know that re-assigning bin[x] will give me a single value that's because I didn't know it as a beginner.
    Instead of taking offense, try to look at it from his point of view -
    Code:
    bin[x]=scanf("%d", &bin[x]);
    He probably thought that you would know about the return value of scanf() if you were going to use it in your code. Maybe you didn't understand what the return value of scanf() meant, but you could have tested your own assumption about what scanf() would return.

    Test your assumptions when you debug. Then you won't be accused of guess work when you are stuck next time.

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Sankait Laroiya View Post
    Secondly, I come here only after trying and eliminating all the bugs under my knowledge.
    Based on this thread, and the other one you just started, I think you should read the following link:

    A development process

    It describes how to write a program a little bit at a time, testing along the way.

    For instance, if the first thing you need to do is get user input, then write code only to get the user input and print it out to the screen. Compile and test, verifying that you have read the expected values correctly. Then you can continue adding code, a little bit at a time, compiling and testing as you go.

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Sankait Laroiya View Post
    So, if I didn't know that re-assigning bin[x] will give me a single value that's because I didn't know it as a beginner.
    Oh, give me a break. You're a beginner who makes guesses, assumes they are right, is flummoxed when they are wrong, and resorts to begging for help rather than trying things out.

    When something goes wrong in your code, you need to do what whiteflags suggests: test your assumptions and guesses. At the least, you'll learn what guesses are right.

    Software is used to do things that have never been implemented in any other way. That wouldn't happen if everyone insisted they could only do things they already know about, or if everyone relied on being able to guess the right answer, first time every time.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [BEGINNER] Decimal to Binary Converter help
    By Sankait Laroiya in forum C Programming
    Replies: 7
    Last Post: 09-11-2014, 10:10 AM
  2. Binary to Decimal Converter
    By Rocket Power in forum C Programming
    Replies: 1
    Last Post: 02-26-2013, 05:05 PM
  3. Replies: 7
    Last Post: 12-03-2012, 04:42 AM
  4. Help with a decimal to binary converter
    By danielerasmus in forum C++ Programming
    Replies: 7
    Last Post: 02-23-2008, 11:37 AM
  5. Decimal to Binary Converter
    By peckitt99 in forum C Programming
    Replies: 16
    Last Post: 10-12-2006, 05:25 AM